package com.xr.redis.util;

import com.xr.redis.cache.service.ICacheService;
import com.xr.redis.holder.SpringContextHolder;

/**
 * <p>
 * 缓存工具类
 * </p>
 *
 * @author Arnold Yang
 * @since 2018-06-22
 */
public class CacheManager {

  /**
   * 获取缓存服务实例
   * @return
   */
  public static ICacheService getCacheService() {
    ICacheService cacheService = SpringContextHolder.getBean(ICacheService.class);
    if(cacheService == null){
      throw new RuntimeException("缓存服务未加载");
    }
    return cacheService;
  }

  /**
   * 缓存数据
   * @param key
   * @param value
   * @throws Exception
   */
  public static void set(String key, Object value) throws Exception {
    getCacheService().set(key, value);
  }


  /**
   * 查询数据
   * @param key
   * @param <T>
   * @return
   * @throws Exception
   */
  public static <T> T get(String key) throws Exception {
    return getCacheService().get(key);
  }
}
